<?php

namespace App\Http\Controllers\Backstage\System;

use App\Lib\Util\ResponseUtil;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rule;
use App\Services\MenuPermissionService;
use App\Goods;
use App\MenuPermission;
use App\Lib\Auth\UserProfileHelper;
use App\Services\GoodsService;

class GoodsController extends Controller
{
    private $goodService = null;
    private $userProfileHelper = null;
    private $menuPermissionService = null;

    public function __construct(GoodsService $goodsService,
                                MenuPermissionService $menuPermissionService,
                                UserProfileHelper $userProfileHelper)
    {
        $this->goodService = $goodsService;
        $this->menuPermissionService = $menuPermissionService;
        $this->userProfileHelper = $userProfileHelper;
    }

    public function index()
    {
        return view('backstage.system.goods.goods');
    }

    public function query(Request $request)
    {

            $input = $request->all();

            return $this->goodService->getGoods($input);

        return [];
    }

    public function create()
    {
        return view('backstage.system.goods.create');
    }

    public function saveNew(Request $request)
    {


            $input = $request->all();
//            Goods::create($input);
            $this->goodService->saveGoods($input);

            return response()->json([
                'Success' => true,
                'Message' => '新增商品成功',
            ]);
    }

    private function validateWhenSaveNew(Array $input)
    {
        return Validator::make($input, [
            'commodity_name' => [
                'required',
                'max:50',
                Rule::unique('Goods')->where(function ($query) use($input) {
                    $query->where('commodity_name', $input['commodity_name']);
                })
            ],
            'description' => 'max:100'
        ], [
            'commodity_name.required' => '角色名称必须存在',
            'commodity_name.max' => '角色名称长度不可超过:max',
            'commodity_name.unique' => '角色名称不可重复',
        ])->validate();
    }

    /*public function delete(Request $request)
    {
        $input = $request->all();

        $this->goodService->deleteGoods($input['id']);

        return response()->json([
            'Success' => true,
            'Message' => '删除商品成功',
        ]);
    }*/

    public function delete(Request $request)
    {
        $input = $request->all();
        $goods = Goods::findOrFail($input['id']);
        $goods->delete();

        return response()->json([
            'Success' => true,
            'Message' => '删除路由成功',
        ]);
    }

    public function edit($commodityId)
    {
        $goods = Goods::findOrFail($commodityId);
        return view('backstage.system.goods.create', [
            'goods' => $goods
        ]);
    }

    public function update(Request $request)
    {

        $input = $request->all();

        $goods = Goods::findOrFail($input['id']);

        $goods->update($input);

        return response()->json([
            'Success' => true,
            'Message' => '保存修改商品成功',
        ]);
    }
}
